<!-- 出勤记录 -->
<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            label-width="68px"
        >
            <el-form-item label="员工姓名" prop="userName">
                <el-input
                    v-model="queryParams.userName"
                    placeholder=""
                    clearable
                    size="small"
                />
            </el-form-item>
            <el-form-item label="身份号" prop="idCard">
                <el-input
                    v-model="queryParams.idCard"
                    placeholder=""
                    clearable
                    size="small"
                />
            </el-form-item>
            <el-form-item label="所属单位" prop="companyId">
                <treeselect
                    v-model="queryParams.companyId"
                    style="width: 160px;"
                    :options="deptOptions"
                    :disable-branch-nodes="false"
                    :show-count="true"
                    placeholder="请选择所属单位"
                />
            </el-form-item>
            <el-form-item label="出勤天数" prop="workDays">
                <el-input
                    v-model="queryParams.workDays"
                    placeholder=""
                    clearable
                    size="small"
                />
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >搜索</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    >新增</el-button
                >
            </el-col>
            <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除</el-button>
      </el-col> -->
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    >导出</el-button
                >
            </el-col>
        </el-row>

        <el-table
            v-loading="loading"
            :data="AttendList"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="员工姓名" align="center" prop="userName" />
            <el-table-column label="性别" align="center" prop="sex" />
            <el-table-column label="身份证号" align="center" prop="idCard" />
            <el-table-column
                label="所属单位"
                align="center"
                prop="companyName"
            />
            <el-table-column label="出勤天数" align="center" prop="workDays" />
            <el-table-column
                label="最后打卡时间-进入"
                align="center"
                prop="inTime"
            />
            <el-table-column
                label="最后打卡时间-外出"
                align="center"
                prop="outTime"
            />
            <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
            >
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="queryDetail(scope.row)"
                        >详情</el-button
                    >
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改岗位对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="500px"
            append-to-body
        >
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="员工编号" prop="userId">
                    <el-input
                        v-model="form.userId"
                        placeholder="请输入牌号"
                        maxlength="30"
                    />
                </el-form-item>
                <el-form-item label="绑定芯片" prop="chipCard">
                    <el-input
                        v-model="form.chipCard"
                        placeholder="请输入芯片编号"
                        maxlength="30"
                    />
                </el-form-item>
                <el-form-item label="基站编号" rop="siteNo">
                    <el-select
                        v-model="form.siteNo"
                        placeholder="请选择"
                        :style="{ width: '100%' }"
                    >
                        <el-option
                            v-for="item in StationList"
                            :key="item.siteNo"
                            :label="item.siteName"
                            :value="item.siteNo"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="打卡类型">
                    <el-select
                        v-model="form.punchType"
                        placeholder="请选择"
                        :style="{ width: '100%' }"
                    >
                        <el-option
                            v-for="item in punchTypeOptions"
                            :key="item.dictCode"
                            :label="item.dictLabel"
                            :value="item.dictValue"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="进出类型">
                    <el-select
                        v-model="form.inoutType"
                        placeholder="请选择"
                        :style="{ width: '100%' }"
                    >
                        <el-option
                            v-for="item in inoutTypeOptions"
                            :key="item.dictCode"
                            :label="item.dictLabel"
                            :value="item.dictValue"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="进出时间" prop="inoutTime">
                    <el-date-picker
                        v-model="form.inoutTime"
                        type="datetime"
                        format="yyyy-MM-dd HH:mm:ss"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        :style="{ width: '100%' }"
                        placeholder="请选择"
                        clearable
                    >
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 添加或修改岗位对话框 -->
        <el-dialog
            title="考勤详情"
            :visible.sync="inoutTable"
            width="750px"
            append-to-body
        >
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple2"
                        @click="handleDelete"
                        v-hasPermi="['system:post:remove']"
                        >删除</el-button
                    >
                </el-col>
            </el-row>
            <el-table
                v-loading="loading"
                :data="AttendDetailList"
                @selection-change="handleSelectionChange2"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column
                    label="员工姓名"
                    align="center"
                    prop="userName"
                />
                <el-table-column
                    label="芯片编号"
                    align="center"
                    prop="chipCard"
                />
                <el-table-column
                    label="基站名称"
                    align="center"
                    prop="siteName"
                />
                <el-table-column
                    label="进出类型"
                    align="center"
                    prop="inoutType"
                />
                <el-table-column
                    label="打卡时间"
                    align="center"
                    prop="inoutTime"
                />
                <el-table-column
                    label="打卡类型"
                    align="center"
                    prop="punchType"
                />
            </el-table>
            <pagination
                v-show="detailtotal > 0"
                :total="detailtotal"
                :page.sync="detailParams.pageNum"
                :limit.sync="detailParams.pageSize"
                @pagination="getAttendDetailList"
            />
        </el-dialog>
    </div>
</template>

<script>
import { getAttendList, attendRecordAdd, getAttendDetailList, exportAttendRecord, delAttendDetail } from "@/api/check/check"
import { getStationList } from "@/api/car/car"
//单位树
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
    name: "check_record",
    components: { Treeselect },
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            ids2: [],
            single2: true,
            multiple2: true,
            detailtotal: 0,
            // 考勤记录数据
            AttendList: [],
            // 考勤记录详情数据
            AttendDetailList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            inoutTable: false,
            // 打卡类型数据字典
            punchTypeOptions: [],
            inoutTypeOptions: [],
            userSexOptions: [],
            //基站列表
            StationList: [],
            // 部门树选项
            deptOptions: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                companyId: undefined,
                idCard: '',
                userName: '',
                workDays: ''
            },
            // 考勤详情查询参数
            detailParams: {
                userId: '',
                pageNum: 1,
                pageSize: 10
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                userId: [
                    { required: true, message: "员工编号不能为空", trigger: "blur" }
                ],
                chipCard: [
                    { required: true, message: "芯片编号不能为空", trigger: "blur" }
                ],
                siteNo: [
                    { required: true, message: "基站编号不能为空", trigger: "blur" }
                ]
            }
        };
    },
    created () {
        this.getTreeselect();
        //获取打卡类型
        this.getDicts("punchType").then(response => {
            this.punchTypeOptions = response.data;
        });
        //进出类型字典表
        this.getDicts("inoutType").then(response => {
            this.inoutTypeOptions = response.data;
        });
        //获取用户性别
        this.getDicts("sys_user_sex").then(response => {
            this.userSexOptions = response.data;
        });
        this.getList();
    },
    methods: {
        inoutTypeFormatter (row) {
            return this.selectDictLabel(this.inoutTypeOptions, row.inoutType);
        },
        punchTypeFormatter (row) {
            return this.selectDictLabel(this.punchTypeOptions, row.punchType);
        },
        /** 查询部门下拉树结构 */
        getTreeselect () {
            treeselect().then(response => {
                this.deptOptions = response.data;
            });
        },
        /** 查询基站列表 */
        getStationList () {
            getStationList().then(response => {
                this.StationList = response.data;
            });
        },
        /** 查询考勤记录列表 */
        getList () {
            this.loading = true;
            getAttendList(this.queryParams).then(response => {
                this.AttendList = response.data;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 查询考勤记录详情 */
        getAttendDetailList () {
            this.loading = true;
            getAttendDetailList(this.detailParams).then(response => {
                this.AttendDetailList = response.data;
                this.total = response.total;
                this.loading = false;
            }
            );
        },
        // 用户性别字典翻译
        userSexFormat (row, column) {
            for (let i = 0; i < this.userSexOptions.length; i++) {
                if (row.sex == this.userSexOptions[i].dictValue) {
                    return this.userSexOptions[i].dictLabel
                }
            }
        },
        // 取消按钮
        cancel () {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset () {
            this.form = {
                chipCard: undefined,
                inoutTime: undefined,
                inoutType: undefined,
                punchType: undefined,
                siteNo: undefined,
                userId: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange (selection) {
            this.ids = selection.map(item => item.postId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        // 详情列表多选框选中数据
        handleSelectionChange2 (selection) {
            this.ids2 = selection.map(item => item.id);
            this.single2 = selection.length != 1;
            this.multiple2 = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd () {
            this.reset();
            this.getStationList();  //查询基站列表
            this.open = true;
            this.title = "添加考勤记录";
        },
        /** 查询详情 */
        queryDetail (row) {
            this.detailParams.userId = row.userId;
            this.getAttendDetailList();
            this.inoutTable = true;
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    attendRecordAdd(this.form).then(response => {
                        if (response.code === 200) {
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }
                    });
                }
            });

        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const postIds = row.postId || this.ids2;
            // console.log(postIds)
            this.$confirm('是否确认删除此数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delAttendDetail(postIds);
            }).then(() => {
                this.getAttendDetailList(); //刷新考勤详情数据
                this.msgSuccess("删除成功");
            }).catch(function () { });
        },
        /** 导出按钮操作 */
        handleExport () {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出出勤记录?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return exportAttendRecord(queryParams);
            }).then(response => {
                this.download(response.msg);
            }).catch(function () { });
        }
    }
};
</script>